/**
 * Rule
 *
 * @author jprieur
 * @created Mar. 20 2007
 */
package mygui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import kernel.Simulator;

public class Rule extends JComponent
{
    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;
    public static final int SIZE = 35;

    private int _orientation;
    private int _increment;
    private int _units;
    private int _size;
    private DisplayPanel _display;

    public Rule(int orientation, DisplayPanel display)
    {
	_orientation = orientation;
	_increment = DisplayPanel.GRID_STEP_SIZE;
	_units = 1;
	_display = display;
    }

    public void setPreferredHeight(int ph) {
        setPreferredSize(new Dimension(SIZE, ph));
    }

    public void setPreferredWidth(int pw) {
        setPreferredSize(new Dimension(pw, SIZE));
    }

    protected void paintComponent(Graphics g) {
	_units = _display.getZoom();
	_increment = DisplayPanel.GRID_STEP_SIZE * _display.getZoom();
	int size = (_orientation == HORIZONTAL) ? Simulator.getTailleX() : Simulator.getTailleY();
        size *= _display.getZoom();
	
	Rectangle drawHere = g.getClipBounds();

        g.setColor(new Color(240, 255, 240));
        g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);

        // Do the ruler labels in a small font that's black.
        g.setFont(new Font("SansSerif", Font.PLAIN, 10));
        g.setColor(Color.black);

        // Some vars we need.
        int end = size + 1;
        int start = 0;
        int tickLength = 0;
        String text = null;

        // Use clipping bounds to calculate first and last tick locations.
        /*if (_orientation == HORIZONTAL) {
            start = (drawHere.x / _increment) * _increment;
            end = (((drawHere.x + drawHere.width) / _increment) + 1)
                  * _increment;
        } else {
            start = (drawHere.y / _increment) * _increment;
            end = (((drawHere.y + drawHere.height) / _increment) + 1)
                  * _increment;
		  }*/
	
	//end = _size + 1;

        // Make a special case of 0 to display the number
        // within the rule and draw a _units label.
        if (start == 0) {
            tickLength = 10;
            if (_orientation == HORIZONTAL) {
                g.drawLine(0, SIZE-1, 0, SIZE-tickLength-1);
            } else {
                g.drawLine(SIZE-1, 0, SIZE-tickLength-1, 0);
            }
            start = _increment;
        }

        // ticks and labels
        for (int i = start; i < end; i += _increment) {
            if (i % _units == 0)  {
                tickLength = 10;
                text = Integer.toString(i/_units);
            } else {
                tickLength = 7;
                text = null;
            }

            if (tickLength != 0) {
                if (_orientation == HORIZONTAL) {
                    g.drawLine(i, SIZE-1, i, SIZE-tickLength-1);
                    if (text != null)
                        g.drawString(text, i-3, 21);
                } else {
                    g.drawLine(SIZE-1, i, SIZE-tickLength-1, i);
                    if (text != null)
                        g.drawString(text, 9, i+3);
                }
            }
        }
    }
}